<!-- -->
<template>
  <div class="public">
    <div class="header">
      <p>
        <van-icon name="arrow-left" @click="onClickLeft" />
      </p>
      <span>房东</span>
      <p></p>
    </div>
    <div class="con" ref="msgBox">
      <div
        v-for="(item,idx) in dates"
        :key="idx"
        :class="`${item.identity === identity ? 'you' : 'zuo'}`"
      >
        <div>
          <div class="info">
            <div class="info-content">{{ item.usernews }}</div>
          </div>
        </div>
      </div>
    </div>
    <div class="footer">
      <div class="ipt">
        <input type="text" v-model="value" />
      </div>
      <button @click="lickfasong">发送</button>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  // import引入到组件需要注入到对象中才能使用
  components: {},
  data() {
    // 这里存放数据
    return {
      value: "",
      dates: [],
      identity: ""
    };
  },
  // 监听属性 类似于data概念
  computed: {
    ...mapState(["userId"])
  },
  // 监听data中的数据变化
  watch: {},

  //生命周期 - 创建完成 (可以访问当前this实例)
  created() {
    this.identity = localStorage.getItem("identity");
  },
  //生命周期 - 挂载完成 (可以访问DOM元素)
  mounted() {
    this.init();
  },
  // 方法集合

  methods: {
    init() {
      if (typeof WebSocket === "undefined") {
        alert("您的浏览器不支持socket");
      } else {
        // 实例化socket
        this.socket = new WebSocket("ws://10.3.221.23:3000");
        // 监听socket连接
        this.socket.onopen = this.open;
        // 监听socket错误信息
        this.socket.onerror = this.error;
        // 监听socket消息
        this.socket.onmessage = this.getMessage;
      }
    },
    open: function() {
      console.log("socket连接成功");
    },
    error: function() {
      console.log("连接错误");
    },
    // msg后端返回的data数据
    getMessage: function(msg) {
      let data = JSON.parse(msg.data);
      data.sort((a, b) => {
        return a.id - b.id;
      });
      this.dates = data;
      console.log(this.dates);

      // 当页面的dom节点创建完毕后  走这个方法 宏任务
      this.$nextTick(() => {
        console.dir(this.$refs.msgBox);
        const ScrollH = this.$refs.msgBox.scrollHeight; // 聊天内容的总高度
        const ClientH = this.$refs.msgBox.clientHeight; //视口高度
        const ScrollTop = this.$refs.msgBox.scrollTop; // 滚动条距离顶部的高度

        console.log(ScrollH - ClientH - ScrollTop);

        if (ScrollH - ClientH - ScrollTop < 300) {
          console.log("123");
          this.$refs.msgBox.scrollTop = ScrollH - ClientH;
        }
      });
    },
    send: function(params) {
      this.socket.send(params);
    },
    close: function() {
      console.log("socket已经关闭");
    },
    onClickLeft() {
      this.$router.go(-1);
    },
    // 发送
    lickfasong() {
      this.send(
        JSON.stringify({
          userId: this.userId,
          brokerId: this.$route.params.ids,
          value: this.value,
          identity: localStorage.getItem("identity")
        })
      );
      this.value = "";
    }
  }
};
</script> 

<!-- Add "scoped" attribute to limit CSS to this component only -->
<!--使用了scoped属性之后，父组件的style样式将不会渗透到子组件中，-->
<!--然而子组件的根节点元素会同时被设置了scoped的父css样式和设置了scoped的子css样式影响，-->
<!--这么设计的目的是父组件可以对子组件根元素进行布局。-->
<style lang='scss'  scoped>
.header {
  width: 100%;
  height: 50px;
  background: steelblue;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}
.con {
  flex: 1;
  overflow-y: auto;
}
.footer {
  border-top: 1px solid #ccc;
  width: 100%;
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;

  button {
    margin: 0 20px;
    background: green;
    color: #fff;
    border-radius: 5px;
    height: 70%;
    padding: 3px 6px;
    border: none;
  }
  .ipt {
    flex: 1;
    border: 1px solid #ccc;
    height: 70%;
    margin-left: 15px;
    input {
      border: none;
      text-indent: 15px;
      height: 100%;
    }
  }
}

.zuo {
  display: flex;
  margin-bottom: 60px;
  .info {
    // width: 47%;
    margin-left: 10px;
    .info-content {
      word-break: break-all;
      // max-width: 45%;
      display: inline-block;
      padding: 10px;
      font-size: 14px;
      background: #fff;
      position: relative;
      margin-top: 8px;
      background: #dbdbdb;
      border-radius: 4px;
    }
    //小三角形
    .info-content::before {
      position: absolute;
      left: -8px;
      top: 8px;
      content: "";
      border-right: 10px solid #dbdbdb;
      border-top: 8px solid transparent;
      border-bottom: 8px solid transparent;
    }
  }
}

.you {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 60px;
  .info {
    // width: 90%;
    // margin-left: 10px;
    text-align: right;
    // position: relative;
    display: flex;
    align-items: flex-end;
    flex-wrap: wrap;
    flex-direction: column;
    .info-content {
      word-break: break-all;
      // max-width: 45%;
      padding: 10px;
      font-size: 14px;
      // float: right;
      margin-right: 10px;
      position: relative;
      margin-top: 8px;
      background: #a3c3f6;
      text-align: left;
      border-radius: 4px;
    }
    //小三角形
    .info-content::after {
      position: absolute;
      right: -8px;
      top: 8px;
      content: "";
      border-left: 10px solid #a3c3f6;
      border-top: 8px solid transparent;
      border-bottom: 8px solid transparent;
    }
  }
}
</style>
